﻿from PySide6.QtCore import Qt, QSettings, Signal, Slot
from PySide6.QtWidgets import QDialog
from Funcs.Config import Config

# 导入我们生成的界面
from Forms.SendCommandWindow_UI import Ui_FormSendCommand

class SendCommandWindow(QDialog, Ui_FormSendCommand):
    SendCommandTriggerd = Signal(str)

    def __init__(self, parent = None):
        super().__init__()
        self.setupUi(self)
        self.__DeviceDisplayName = ""

        flags = self.windowFlags()
        self.setWindowFlags(flags & ~Qt.WindowContextHelpButtonHint)

    def SetDeviceName(self, deviceDisplayName):
        self.__DeviceDisplayName = deviceDisplayName

        s = QSettings(Config.ConfigIniPath(), QSettings.IniFormat)
        s.beginGroup("UI")
        s.beginGroup(self.__DeviceDisplayName)
        s.beginGroup("SendCommandWindow")

        for i in range(1,11):
            if not s.value("Command" + str(i)) is None:
                plainTextEdit = eval("self.plainTextEdit_Command" + str(i))
                plainTextEdit.setPlainText(s.value("Command" + str(i)))

        s.endGroup()
        s.endGroup()
        s.endGroup()

    def SetDeviceIP(self, ip):
        self.setWindowTitle("发送命令" + (":" + ip if len(ip) > 0 else ""))

    def saveCommand(self, i):
        s = QSettings(Config.ConfigIniPath(), QSettings.IniFormat)
        s.beginGroup("UI")
        s.beginGroup(self.__DeviceDisplayName)
        s.beginGroup("SendCommandWindow")
        plainTextEdit = eval("self.plainTextEdit_Command" + str(i))
        s.setValue("Command" + str(i), plainTextEdit.toPlainText())
        s.endGroup()
        s.endGroup()
        s.endGroup()

    @Slot()
    def on_pushButton_Send1_clicked(self):
        self.saveCommand(1)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command1.toPlainText())
        
    @Slot()
    def on_pushButton_Send2_clicked(self):
        self.saveCommand(2)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command2.toPlainText())
        
    @Slot()
    def on_pushButton_Send3_clicked(self):
        self.saveCommand(3)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command3.toPlainText())
        
    @Slot()
    def on_pushButton_Send4_clicked(self):
        self.saveCommand(4)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command4.toPlainText())
        
    @Slot()
    def on_pushButton_Send5_clicked(self):
        self.saveCommand(5)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command5.toPlainText())
        
    @Slot()
    def on_pushButton_Send6_clicked(self):
        self.saveCommand(6)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command6.toPlainText())
        
    @Slot()
    def on_pushButton_Send7_clicked(self):
        self.saveCommand(7)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command7.toPlainText())
        
    @Slot()
    def on_pushButton_Send8_clicked(self):
        self.saveCommand(8)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command8.toPlainText())
        
    @Slot()
    def on_pushButton_Send9_clicked(self):
        self.saveCommand(9)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command9.toPlainText())
        
    @Slot()
    def on_pushButton_Send10_clicked(self):
        self.saveCommand(10)
        self.SendCommandTriggerd.emit(self.plainTextEdit_Command10.toPlainText())
        
    @Slot()
    def on_closeButton_clicked(self):
        self.reject()


